package com.atguigu.common.result;

import lombok.Data;

/**
 * @version 1.0
 * @autor 雷记新
 */
@Data
public class Result<T> {
    private Integer code;//返回的状态码
    private String message;//返回提示的信息
    private T data;//返回的数据

    private Result(){}

    // 封装返回是数据
    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<T>();
        //封装数据
        if (data != null){
            result.setData(data);
        }
        //状态码
        result.setCode(resultCodeEnum.getCode());
        //返回的信息
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //成功无数据
    public static<T> Result<T> ok(){
        return build(null,ResultCodeEnum.SUCCESS);
    }

    //成功有数据
    public static<T> Result<T> ok(T data){
        return build(data,ResultCodeEnum.SUCCESS);
    }

    //失败无数据
    public static<T> Result<T> fail(){
        return build(null,ResultCodeEnum.FAIL);
    }

    //失败有数据
    public static<T> Result<T> fail(T data){
        return build(data,ResultCodeEnum.FAIL);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
